package com.future.mayi.activity;

import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.bigkoo.pickerview.OptionsPickerView;
import com.future.baselib.utils.ApiCst;
import com.future.baselib.utils.HttpRequest;
import com.future.mayi.R;
import com.future.mayi.app.App;
import com.future.mayi.model.BankModel;
import com.future.mayi.response.BankListResponse;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class AndBankCardActivity extends BaseTitleActivity {
    @BindView(R.id.tv_bank_name)
    TextView tvBankName;
    @BindView(R.id.tv_kaihu_name)
    EditText tvKaihuName;
    @BindView(R.id.tv_car_num)
    EditText tvCarNum;
    @BindView(R.id.tv_enter)
    TextView tvEnter;

    private BankModel bankModel;

    private List<String> bankList;

    @Override
    protected void initTopBar() {
        initBack();
        getTitleText().setText("编辑银行卡");
    }

    @Override
    protected void initLayout() {
        setContentView(R.layout.activity_and_bank_card);
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initDates() {

        if (getIntent() != null) {
            bankModel = (BankModel) getIntent().getSerializableExtra("bankCardModel");
        }
        if (bankModel == null)
            bankModel = new BankModel();
        else {
            tvBankName.setText(bankModel.getBank_name());
            tvKaihuName.setText(bankModel.getReal_name());
            tvCarNum.setText(bankModel.getAccount_name());
        }

        getBankList();
    }

    @Override
    protected void initListener() {

    }

    private OptionsPickerView bankDialog;
    public List<BankModel> list;

    private void selectBank() {
        if (list == null) {
            getBankList();
            toast("正在获取银行列表");
            return;
        }
        if (bankDialog == null) return;
        bankDialog.show();
    }


    public void getBankList() {
        new HttpRequest<BankListResponse>()
                .with(this).setApiCode(ApiCst.getBankList)
                .addParam("token", App.getToken(mContext))
                .setListener(new HttpRequest.OnNetworkListener<BankListResponse>() {
                    @Override
                    public void onSuccess(BankListResponse response) {
//                        list = response.list;
                        if (list == null || list.size() == 0) {
                            bankList = new ArrayList<>(1);
                        } else {
                            bankList = new ArrayList<>(list.size());
                            for (BankModel bankModel : list) {
                                bankList.add(bankModel.getBank_name());
                            }
                        }
                        if (bankDialog == null) {
                            bankDialog = new OptionsPickerView.Builder(AndBankCardActivity.this, new OptionsPickerView.OnOptionsSelectListener() {
                                @Override
                                public void onOptionsSelect(int options1, int options2, int options3, View v) {
                                    dissmissLoadingDialog();
                                    bankModel.setBank_name(list.get(options1).getBank_name());
                                    bankModel.setBank_logo(list.get(options1).getBank_logo());
                                    bankModel.setBank_id(list.get(options1).getBank_id());
                                    tvBankName.setText(bankList.get(options1));
                                }
                            }).setCancelColor(getResources().getColor(R.color.main_text_color))
                                    .setSubmitColor(getResources().getColor(R.color.main_color))
                                    .setContentTextSize(20)
                                    .setTitleText("请选择银行")
                                    .build();
                        }
                        bankDialog.setPicker(bankList);
                    }

                    @Override
                    public void onFail(String message, BankListResponse response) {
                        toast(message);
                    }
                }).start(new BankListResponse());

    }


    @OnClick({R.id.ll_bank_name, R.id.tv_enter})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_bank_name:
                selectBank();
                break;
            case R.id.tv_enter:
                if (TextUtils.isEmpty(tvBankName.getText()) || bankModel == null) {
                    toast("请选择银行卡名称");
                    return;
                }

                if (TextUtils.isEmpty(tvKaihuName.getText())) {
                    toast("请输入开户名");
                    return;
                }
                bankModel.setReal_name(tvKaihuName.getText().toString());

                if (TextUtils.isEmpty(tvCarNum.getText())) {
                    toast("请输入银行卡卡号");
                    return;
                }
                bankModel.setAccount_name(tvCarNum.getText().toString());

                Intent intent = new Intent();
                intent.putExtra("bankModel", bankModel);
                setResult(Activity.RESULT_OK, intent);
                finish();
                break;
        }
    }
}
